<template>
	<view class="push">
		<block v-if="!boxHide">
			<view class="add_prompt">入库信息</view>
			<view class="add_item">
				<view>状态</view>
				<picker mode="selector" :range="typeList" @change="getPickerValue" class="add_picker" :data-index="1">
					<text>{{upJson.status}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			<view class="add_item">
				<view>类别</view>
				<picker mode="selector" :range="classList" @change="getPickerValue" class="add_picker" :data-index="2">
					<text>{{upJson.class}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			<view class="add_item">
				<view>入库单号</view>
				<input type="text" v-model="upJson.order_no" placeholder="如不填写系统将自动生成"/>
			</view>
			<view class="add_item">
				<view>入库时间</view>
				<picker mode="date" @change="getPickerValue" class="add_picker" :data-index="3">
					<text>{{upJson.into_at}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			<view class="add_item">
				<view>发票号</view>
				<input type="text" v-model="upJson.invoice" placeholder="请输入发票号"/>
			</view>
			<view class="add_textarea">
				<view>备注</view>
				<textarea v-model="upJson.desc" placeholder="请输入备注" />
			</view>
			
			<view class="add_item">
				<view>供应商</view>
				<picker mode="selector" :range="supplierList" @change="getPickerValue" class="add_picker" :data-index="5" :range-key="'supplier_name'">
					<text>{{supplier_name}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			<view class="add_item">
				<view>采购员</view>
				<picker mode="selector" :range="staffList" @change="getPickerValue" class="add_picker" :data-index="6" :range-key="'username'">
					<text>{{purchase_user_name}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			<view class="add_item">
				<view>录入人</view>
				<text>{{inputPerson.name}}</text>
			</view>
			<view class="add_icon">
				<view>入库产品</view>
				<view @click.stop="openSearch('product')">
					<!-- <image src="/static/images/code.png"></image> -->
					<image src="/static/images/add.png"></image>
				</view>
			</view>
			
			<block v-for="(item,index) in upJson.product_list">
				<view class="add_item" :key="index">
					<view>产品名称</view>
					<view class="add_picker">
						<text>{{item.name}}</text>
						<image src="/static/images/visit_icon4.png" class="delete" @tap="delProduct(index)"></image>
					</view>
				</view>
				<view class="add_item">
					<view>数量</view>
					<input type="text" v-model="upJson.product_list[index].number" placeholder="请输入数量" @blur="mathPrice(item)"/>
				</view>
				<view class="add_item">
					<view>入库价</view>
					<input type="text" v-model="upJson.product_list[index].price" placeholder="请输入入库价" @blur="mathPrice(item)"/>
				</view>
				<view class="add_item">
					<view>总价（元）</view>
					<input type="text" v-model="upJson.product_list[index].total_price" disabled placeholder="请输入总价"/>
				</view>
				<view class="add_item">
					<view>库房</view>
					<picker mode="selector" :range="upJson.product_list[index].houseList" @change="getPickerValue" :range-key="'title'" class="add_picker" :data-index="8" :data-indexs="index">
						<text>{{item.warehouse_name}}</text>
						<image src="/static/images/right.png" class="right"></image>
					</picker>
				</view>
			</block>
			<!-- <view class="add_prompt">付款计划</view> -->
			<view class="save" @tap="submit()">完成</view>
		</block>
		<Loading ref='load'></Loading>
	</view>
</template>

<script>
	import Time from "../../../components/time/time.vue"
	import {
		deepClone
	} from '@/static/js/index.js'
	export default{
		components:{
			Time
		},
		data(){
			return{
				typeList:[],
				classList:[],
				supplierList:[],
				houseList:[],
				productList:[],
				staffList:[],
				upJson:{
					order_no:"",
					// customer_id:"",
					supplier_id:"",
					desc:"",
					into_at:"",
					invoice:"",
					status:"",
					class:"",
					purchase_user_id:"",
					action_user_id:"",
					product_list:[],
					total_price:""
				},
				supplier_name:"",
				customer_name:"",
				purchase_user_name:"",
				inputPerson:{
					name:"",
					id:""
				},
			}
		},
		
		onLoad() {
			//录入人员信息
			this.inputPerson={
				name:this.$store.getters.userInfo.username,
				id:this.$store.getters.userInfo.id
			};
			this.upJson.action_user_id=this.inputPerson.id;
			this.$refs.load.initLoading({loadType:1,loadPrompt:false}).then(res =>{
				this.boxHide = false
				this.$refs.load.getLoadingData({oldData:this.list,newData:res.data}).then(res => {
				});
			})
			this.$api.getProductIntoList({status:0}).then(res =>{
				this.typeList = res;
				this.upJson.status = this.typeList[0];
			})
			this.$api.getProductClass({status:1}).then(res =>{
				this.classList = res;
				this.upJson.class = this.classList[0];
			})
			this.$api.getSupplierList({}).then(res =>{
				this.supplierList = res;
				this.supplier_name = res[0].supplier_name;
				this.upJson.supplier_id = this.supplierList[0].id;
			})
			this.$api.getProductLibrary({}).then(res => {
				this.houseList = res
			})
			this.$api.getUserList({}).then(res =>{
				let list = [];
				for(let i in res){
					for(let j in res[i].user_list){
						list.push(res[i].user_list[j])
					}
				}
				this.staffList = list;
				this.purchase_user_name = this.staffList[0].username;
				this.upJson.purchase_user_id = this.staffList[0].id;
			})
			this.upJson.into_at = this.$api.getNowTime();
		},
		onShow() {
			this.setBaseNews();
		},
		methods:{
			// 设置基本信息
			setBaseNews(){
				let obj=deepClone(this.$store.getters['parameters/searchObj']);
				if(obj.key==="product"){
					// let a = {product_id:obj.data.id,name:obj.data.title,number:1,warehouse_id:this.houseList[0].id,warehouse_name:this.houseList[0].title,desc:"",into_price:0,total_price:0}
					// this.upJson.product_list.push(a);
					let listData=this.upJson.product_list.filter(item=>item.product_id==obj.data.id);
					if(listData.length>0){
						uni.showToast({
							title:"已经选择了该产品",
							icon:"none"
						})
						return false;
					}
					this.$api.getProductWarehouse({product_id:obj.data.id}).then(res => {
						let a = {houseList:res,warehouse_id:res[0].warehouse_id,warehouse_name:res[0].title,product_id:obj.data.id,name:obj.data.title,number:1,price:obj.data.price,total_price:obj.data.price,max:obj.data.stock,desc:""}
						this.upJson.product_list.push(a);
						this.initTotalPrice();
					})
				}
				this.$store.dispatch('parameters/deleteObj');
			},
			//初始化总价
			initTotalPrice(){
				let total_price = 0;
				this.upJson.product_list.forEach(item=>{
					item.total_price=item.number*item.price;
					total_price += item.total_price
				})
			},
			openSearch(key){
				uni.navigateTo({
					url:'/pages/searchList/searchList?searchKey='+key
				})
			},
			delProduct(index){//删除产品
				let _this=this;
				uni.showModal({
					title:'温馨提示',
					content:"确认删除?",
					success(res) {
						if(res.cancel) return false;
						_this.upJson.product_list.splice(index,1);
					}
				})
			},
			getPickerValue(e){
				if(e.currentTarget.dataset.index == 1){
					this.upJson.status = this.typeList[e.detail.value]
				}else if(e.currentTarget.dataset.index == 2){
					this.upJson.class = this.classList[e.detail.value]
				}else if(e.currentTarget.dataset.index == 3){
					this.upJson.into_at = e.detail.value
				}else if(e.currentTarget.dataset.index == 5){
					this.supplier_name = this.supplierList[e.detail.value].supplier_name
					this.upJson.supplier_id = this.supplierList[e.detail.value].id
				}else if(e.currentTarget.dataset.index == 6){
					this.purchase_user_name = this.staffList[e.detail.value].username
					this.upJson.purchase_user_id = this.staffList[e.detail.value].id
				}else if(e.currentTarget.dataset.index == 8){
					this.upJson.product_list[Number(e.currentTarget.dataset.indexs)].warehouse_id = this.upJson.product_list[Number(e.currentTarget.dataset.indexs)].houseList[e.detail.value].warehouse_id;
					this.upJson.product_list[Number(e.currentTarget.dataset.indexs)].warehouse_name = this.upJson.product_list[Number(e.currentTarget.dataset.indexs)].houseList[e.detail.value].title;
				}
			},
			mathPrice(itemObj){
				if(itemObj.number>itemObj.max){
					uni.showToast({
						title:'该产品数量超限,已调整为最大值',
						icon:"none"
					})
					itemObj.number=itemObj.max;
				}else if(itemObj.number==0){
					uni.showToast({
						title:'产品数量不能为0,已调整为最小值1',
						icon:"none"
					})
					itemObj.number=1;
				}
				this.initTotalPrice();
			},
			submit(){
				if(this.upJson.invoice.length < 1){
					uni.showToast({title:"发票号不能为空",icon:"none"})
				}else if(this.upJson.product_list.length < 1){
					uni.showToast({title:"入库产品不能为空",icon:"none"})
				}else{
					let upJson = {
						order_no:this.upJson.order_no,
						supplier_id:this.upJson.supplier_id,
						desc:this.upJson.desc,
						into_at:this.upJson.into_at,
						invoice:this.upJson.invoice,
						status:this.upJson.status,
						class:this.upJson.class,		
						purchase_user_id:this.upJson.purchase_user_id,
						action_user_id:this.upJson.action_user_id,
						product_list:this.upJson.product_list
					};
					upJson.product_list = upJson.product_list.map(item=>{
						return {
							"product_id":item.product_id,
							'number':item.number,
							'warehouse_id':item.warehouse_id,
							"into_price":item.price,
							"total_price":item.total_price,
							'desc':item.desc
						}
					});
					upJson.product_list = JSON.stringify(upJson.product_list)
					this.$api.addIntoLibrary(upJson).then(res=>{
						this.$app.globalData.upData.product=true;
					})
				}
			}
		}
	}
</script>

<style>
	.push{width: 100%;padding-bottom: 50upx;background: #FFFFFF;}
</style>
